<?
function show_login($error) {
    global  $config;
    global  $phpEx;

    $roomlist   = getRoomList(false, false);

    $template = new Template($config['TPL_DIR']);
    $template->set_filenames(array('body' => 'transcript_query.tpl'));
    $username   = '';
    $password   = '';

    if (isset($_REQUEST['username'])) {
        $username   = $_REQUEST['username'];
    }
    if (isset($_REQUEST['password'])) {
        $password   = $_REQUEST['password'];
    }

    $template->assign_vars(array(
        'ROOMLIST'  => $roomlist,
        'ERROR'     => $error,
        'USERNAME'  => $username,
        'PASSWORD'  => $password
    ));

    show_date_select(&$template, '1');
    show_date_select(&$template, '2');

    $template->pparse('body');
}

function show_date_select($template, $suffix) {
    $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    $this_month = date("M");
    $this_year  = date("Y");
    if ($suffix == 1) {
        $this_date  = date("j")-1;
    } else {
        $this_date  = date("j");
    }

    print("<!-- today is $this_month $this_date $this_year -->\n");
    foreach ($months as $m => $n) {
        $month  = $m+1;
        $sel    = '';
        if ($n == $this_month) {
            $sel    = " SELECTED";
        }
        $template->assign_block_vars("month$suffix", array(
            'VALUE'     => $month,
            'NAME'      => $n,
            'SELECTED'  => $sel
        ));
    }

    for ($i = 1; $i < 32; $i++) {
        $sel    = '';
        if ($i == $this_date) {
            $sel    = " SELECTED";
        }
        $template->assign_block_vars("day$suffix", array(
            'VALUE'     => $i,
            'NAME'      => $i,
            'SELECTED'  => $sel
        ));
    }

    for ($i = 2004; $i <= $this_year; $i++) {
        $sel    = '';
        if ($i == $this_year) {
            $sel    = " SELECTED";
        }
        $template->assign_block_vars("year$suffix", array(
            'VALUE'     => $i,
            'NAME'      => $i,
            'SELECTED'  => $sel
        ));
    }

    for ($i = 0; $i < 24; $i++) {
        $j  = str_pad($i, 2, "0", STR_PAD_LEFT);
        $template->assign_block_vars("hour$suffix", array(
            'VALUE'     => $j,
            'NAME'      => $j,
            'SELECTED'  => ''
        ));
    }

    for ($i = 0; $i < 60; $i++) {
        $j  = str_pad($i, 2, "0", STR_PAD_LEFT);
        $template->assign_block_vars("minute$suffix", array(
            'VALUE'     => $j,
            'NAME'      => $j,
            'SELECTED'  => ''
        ));
    }
}

function pull_transcript_events($start, $end, $rid, $filter = 'base') {
    global  $dbprefix;
    global  $db2;

    $sfilter    = '';
    if ($rid != -1) {
        $sfilter    .= "room_id_to=$rid AND\n";
    }
    if ($filter == 'base') {
        $sfilter    .= "user_id_to=0 AND\n";
    }
    $sfilter    .= "event_chat_flags != 4 AND\n";  // skip sound events, no need to send those in transcripts

    $sql    = "SELECT
                *,DATE_FORMAT(event_date, '%m/%d %H:%i') AS event_date_fmt
            FROM
                {$dbprefix}events AS e LEFT JOIN {$dbprefix}rooms AS r
            ON
                (r.room_id = e.room_id_to)
            WHERE
                $sfilter
                (UNIX_TIMESTAMP(e.event_date) > UNIX_TIMESTAMP($start)) AND
                (UNIX_TIMESTAMP(e.event_date) < UNIX_TIMESTAMP($end))
            ORDER BY
                e.event_id";
    print_sql_comment($sql);
    
    if (!($result = $db2->sql_query($sql))) {
        $err    = $db2->sql_error();
        showFatalError("<B>Error #{$err['code']}: {$err['message']}</b>: $str_sql");
    }

    $events = $db2->sql_fetchrowset();
    return $events;
}
